//管理员端首页
package xiangmu.schoolpaper.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xiangmu.schoolpaper.mapper.AdminHomeMapper;
import xiangmu.schoolpaper.pojo.VO.*;
import xiangmu.schoolpaper.service.AdminHomeService;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class AdminHomeServiceImpl implements AdminHomeService {
    @Autowired
    private AdminHomeMapper adminHomeMapper;

//近七天论文上传数量
    @Override
    public List<HomeWeekVO> getWeeklyPaperUploads() {
        return adminHomeMapper.uploadCountOfPapersWeek();
    }
//上传论文总数
    @Override
    public Integer getPaperSum() {
        Integer total = adminHomeMapper.getPaperSum();
        return total;
    }
//上传模板总数
    @Override
    public Integer getTemplateSum() {
        Integer count = adminHomeMapper.getTemplateSum();
        return count;
    }
//论文通过总数
    @Override
    public Integer getPassSum() {
        Integer amount = adminHomeMapper.getPassSum();
        return amount;
    }
//活跃人数
    @Override
    public Integer getActiveUsersSum() {
        Integer number = adminHomeMapper.getActiveUsersSum();
        return number;
    }
    @Override
    public HomeOverviewVO getHomeOverview(){
        return adminHomeMapper.getHomeOverview();
    }
//每个专业论文分数分布情况
    @Override
    public ScatterChartVo getPaperScoreDistribution() {
        // 原始查询数据
        List<HomeEveryStudentScoreVo> rawList = adminHomeMapper.getPaperScoreDistribution();

        // 过滤掉 null 分数
        rawList = rawList.stream()
                .filter(item -> item.getTotalScore() != null)
                .collect(Collectors.toList());

        // 收集所有专业（有序）
        List<String> majors = rawList.stream()
                .map(HomeEveryStudentScoreVo::getMajorName)
                .distinct()
                .collect(Collectors.toList());

        // 构造散点数据
        List<List<Object>> scatterData = rawList.stream()
                .map(item -> {
                    int xIndex = majors.indexOf(item.getMajorName());
                    double jitter = Math.random();
                    return List.of((Object) xIndex, item.getTotalScore(), jitter);
                })
                .collect(Collectors.toList());

        // 封装返回
        ScatterChartVo vo = new ScatterChartVo();
        vo.setMajors(majors);
        vo.setData(scatterData);
        return vo;
    }

//获取每个专业论文总分分布区间的学生数量
    @Override
    public List<HomeScoreStudentSumVO> getAdminHomeScoreStudentSum() {
        return adminHomeMapper.getAdminHomeScoreStudentSum();
    }
}